Sandbox vectors

Let’s define some vectors which can be used for demonstrations:

manyNumbers <- sample( 1:1000, 20 )
manyNumbers
 [1] 214 493 428 994 685 431 134  61 351 986 285 976 181 884 772 999  91 495 893 319
manyNumbersWithNA <- sample( c( NA, NA, NA, manyNumbers ) )
manyNumbersWithNA
 [1] 134 214 976 772 181 319 986  91 493 428  NA 495 285 351  NA 994 685  NA  61 884 431 893 999
duplicatedNumbers <- sample( 1:5, 10, replace = TRUE )
duplicatedNumbers
 [1] 5 2 5 1 5 2 4 5 4 2
letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
LETTERS
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
mixedLetters <- c( sample( letters, 5 ), sample( LETTERS, 5 ) )
mixedLetters
 [1] "t" "e" "p" "a" "g" "P" "S" "G" "K" "J"

Are all/any elements TRUE

all( manyNumbers <= 1000 )
[1] TRUE
all( manyNumbers <= 500 )
[1] FALSE
any( manyNumbers > 1000 )
[1] FALSE
any( manyNumbers > 500 )
[1] TRUE
all( !is.na( manyNumbers ) )
[1] TRUE
any( is.na( manyNumbers ) )
[1] FALSE

Which elements are TRUE

Input: logical vector Output: vector of numbers (positions)

which( manyNumbers > 900 )
[1]  4 10 12 16
which( manyNumbersWithNA > 900 )
[1]  3  7 16 23
which( is.na( manyNumbersWithNA ) )
[1] 11 15 18

Filtering vector elements

manyNumbers[ manyNumbers > 900 ] # indexing by logical vector
[1] 994 986 976 999
manyNumbers[ which( manyNumbers > 900 ) ] # indexing by positions
[1] 994 986 976 999
somePositions <- which( manyNumbers > 900 )
manyNumbers[ somePositions ]
[1] 994 986 976 999

Are some elements among other elements

"A" %in% LETTERS
[1] TRUE
c( "X", "Y", "Z" ) %in% LETTERS
[1] TRUE TRUE TRUE
all( c( "X", "Y", "Z" ) %in% LETTERS )
[1] TRUE
all( mixedLetters %in% LETTERS )
[1] FALSE
any( mixedLetters %in% LETTERS )
[1] TRUE
mixedLetters[ mixedLetters %in% LETTERS ]
[1] "P" "S" "G" "K" "J"
mixedLetters[ !( mixedLetters %in% LETTERS ) ]
[1] "t" "e" "p" "a" "g"
manyNumbers %in% 300:600
 [1] FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE
which( manyNumbers %in% 300:600 )
[1]  2  3  6  9 18 20
sum( manyNumbers %in% 300:600 )
[1] 6

Pick one of two (three) depending on condition

if_else( manyNumbersWithNA >= 500, "large", "small" )
 [1] "small" "small" "large" "large" "small" "small" "large" "small" "small" "small" NA      "small" "small" "small" NA     
[16] "large" "large" NA      "small" "large" "small" "large" "large"
if_else( manyNumbersWithNA >= 500, "large", "small", "UNKNOWN" )
 [1] "small"   "small"   "large"   "large"   "small"   "small"   "large"   "small"   "small"   "small"   "UNKNOWN" "small"  
[13] "small"   "small"   "UNKNOWN" "large"   "large"   "UNKNOWN" "small"   "large"   "small"   "large"   "large"  
# here integer 0L is needed instead of real 0.0 
# manyNumbersWithNA contains integer numbers and the method complains
if_else( manyNumbersWithNA >= 500, manyNumbersWithNA, 0L ) 
 [1]   0   0 976 772   0   0 986   0   0   0  NA   0   0   0  NA 994 685  NA   0 884   0 893 999

Duplicates and unique elements

unique( duplicatedNumbers )
[1] 5 2 1 4
unique( c( NA, duplicatedNumbers, NA ) )
[1] NA  5  2  1  4
duplicated( duplicatedNumbers )
 [1] FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE

Positions of max/min elements

which.max( manyNumbersWithNA )
[1] 23
manyNumbersWithNA[ which.max( manyNumbersWithNA ) ]
[1] 999
which.min( manyNumbersWithNA )
[1] 19
manyNumbersWithNA[ which.min( manyNumbersWithNA ) ]
[1] 61
range( manyNumbersWithNA, na.rm = TRUE )
[1]  61 999

Sorting/ordering of vectors

manyNumbersWithNA
 [1] 134 214 976 772 181 319 986  91 493 428  NA 495 285 351  NA 994 685  NA  61 884 431 893 999
sort( manyNumbersWithNA )
 [1]  61  91 134 181 214 285 319 351 428 431 493 495 685 772 884 893 976 986 994 999
sort( manyNumbersWithNA, na.last = TRUE )
 [1]  61  91 134 181 214 285 319 351 428 431 493 495 685 772 884 893 976 986 994 999  NA  NA  NA
sort( manyNumbersWithNA, na.last = TRUE, decreasing = TRUE )
 [1] 999 994 986 976 893 884 772 685 495 493 431 428 351 319 285 214 181 134  91  61  NA  NA  NA
manyNumbersWithNA[1:5]
[1] 134 214 976 772 181
order( manyNumbersWithNA[1:5] )
[1] 1 5 2 4 3
rank( manyNumbersWithNA[1:5] )
[1] 1 3 5 4 2
sort( mixedLetters )
 [1] "a" "e" "g" "G" "J" "K" "p" "P" "S" "t"

Ranking of vectors

manyDuplicates <- sample( 10:15, 10, replace = TRUE )
rank( manyDuplicates )
 [1] 9.0 6.5 6.5 4.5 9.0 2.5 9.0 1.0 4.5 2.5
rank( manyDuplicates, ties.method = "min" )
 [1] 8 6 6 4 8 2 8 1 4 2
rank( manyDuplicates, ties.method = "random" )
 [1]  8  7  6  5 10  3  9  1  4  2

Rounding numbers

v <- c( -1, -0.5, 0, 0.5, 1, rnorm( 10 ) )
v
 [1] -1.00000000 -0.50000000  0.00000000  0.50000000  1.00000000 -1.43295010 -2.27817614  0.08998173  1.79937099  1.52399766
[11] -0.16566794  0.54867070 -1.15039510  0.52715497  0.94365527
round( v, 0 )
 [1] -1  0  0  0  1 -1 -2  0  2  2  0  1 -1  1  1
round( v, 1 )
 [1] -1.0 -0.5  0.0  0.5  1.0 -1.4 -2.3  0.1  1.8  1.5 -0.2  0.5 -1.2  0.5  0.9
round( v, 2 )
 [1] -1.00 -0.50  0.00  0.50  1.00 -1.43 -2.28  0.09  1.80  1.52 -0.17  0.55 -1.15  0.53  0.94
floor( v )
 [1] -1 -1  0  0  1 -2 -3  0  1  1 -1  0 -2  0  0
ceiling( v )
 [1] -1  0  0  1  1 -1 -2  1  2  2  0  1 -1  1  1

Naming vector elements

heights <- c( Amy = 166, Eve = 170, Bob = 177 )
heights
Amy Eve Bob 
166 170 177 
names( heights )
[1] "Amy" "Eve" "Bob"
names( heights ) <- c( "AMY", "EVE", "BOB" )
heights
AMY EVE BOB 
166 170 177 
heights[[ "EVE" ]]
[1] 170

Generating grids

expand_grid( x = c( 1:3, NA ), y = c( "a", "b" ) )
# A tibble: 8 × 2
      x y    
  <int> <chr>
1     1 a    
2     1 b    
3     2 a    
4     2 b    
5     3 a    
6     3 b    
7    NA a    
8    NA b    

Generating combinations

combn( c( "a", "b", "c", "d", "e" ), m = 2, simplify = TRUE )
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a"  "a"  "a"  "a"  "b"  "b"  "b"  "c"  "c"  "d"  
[2,] "b"  "c"  "d"  "e"  "c"  "d"  "e"  "d"  "e"  "e"  
combn( c( "a", "b", "c", "d", "e" ), m = 3, simplify = TRUE )
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a"  "a"  "a"  "a"  "a"  "a"  "b"  "b"  "b"  "c"  
[2,] "b"  "b"  "b"  "c"  "c"  "d"  "c"  "c"  "d"  "d"  
[3,] "c"  "d"  "e"  "d"  "e"  "e"  "d"  "e"  "e"  "e"  


Copyright © 2022 Biomedical Data Sciences (BDS) | LUMC